package java_thinking.unit_21._21_7;

import java.math.BigInteger;
import java.util.concurrent.BlockingQueue;

/**
 * @Desc:   7-3 不可靠的取消操作将把生产者至于阻塞的操作中 不要这么做
 * @author: cww
 * @DateTime: 2019/6/13 20:31
 */

public class BrokenPrimeProducer extends Thread {
    private final BlockingQueue<BigInteger> queue;
    private volatile boolean cancelled = false;

    public BrokenPrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {

            BigInteger p = BigInteger.ONE;
            while (!cancelled) {
                //put 被阻塞后就无法再取消线程,因为它无法从阻塞的 put 方法中恢复过来
                    queue.put(p = p.nextProbablePrime());

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void cancell() {
        cancelled = true;
    }
}
